package guiTest;

import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.ScrollPaneConstants;

@SuppressWarnings("serial")
public class Test extends JFrame implements KeyListener, ActionListener {
	JTextField input = new JTextField(8);
	JButton send = new JButton("Send");
	JTextArea output = new JTextArea(5, 10);
	Font font = new Font(Font.DIALOG, Font.PLAIN, 26);
	GuiThread thread;
	
	Socket socket = null;
	PrintWriter out = null;
	BufferedReader in = null;
	
	public Test() {
		super();
		setLayout(new BorderLayout(10, 10));
				//GridLayout(3, 1));
		input.setFont(font);
		send.setFont(font);
		output.setFont(font);
		input.addKeyListener(this);
		send.addKeyListener(this);
		output.addKeyListener(this);
		add(input, BorderLayout.NORTH);
		add(send, BorderLayout.CENTER);
		JScrollPane outputSP = new JScrollPane(output);
		outputSP.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
		outputSP.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
		add(outputSP, BorderLayout.SOUTH);
		send.addActionListener(this);
		setVisible(true);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setLocation(400, 400);
		pack();
		
		try {
			socket = new Socket("localhost", 44);
			out = new PrintWriter(socket.getOutputStream(), true);
			in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
		} catch (Exception ex) {}
		
		thread = new GuiThread(this);
	}

	public Test(boolean withoutFrame) {
		super();
		setLayout(new BorderLayout(10, 10));
				//GridLayout(3, 1));
		input.setFont(font);
		send.setFont(font);
		output.setFont(font);
		input.addKeyListener(this);
		send.addKeyListener(this);
		output.addKeyListener(this);
		add(input, BorderLayout.NORTH);
		add(send, BorderLayout.CENTER);
		JScrollPane outputSP = new JScrollPane(output);
		outputSP.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
		outputSP.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
		add(outputSP, BorderLayout.SOUTH);
		send.addActionListener(this);
		//setVisible(true);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setLocation(400, 400);
		pack();
		
		try {
			socket = new Socket("localhost", 44);
			out = new PrintWriter(socket.getOutputStream(), true);
			in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
		} catch (Exception ex) {}
		
		thread = new GuiThread(this);
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		send();
	}

	@Override
	public void keyPressed(KeyEvent e) {
		if (e.getKeyCode() == 10)
			send();
	}
	
	void send() {
		out.println(input.getText());
		input.setText("");
		input.requestFocus();
	}
	void receive() {
		try {
			output.append(in.readLine()+"\n");
		} catch (Exception ex) {}
		input.requestFocus();
	}
	
	@Override
	public void keyReleased(KeyEvent arg0) {}
	@Override
	public void keyTyped(KeyEvent arg0) {}

	public static void main(String[] args) {
		Test t = new Test();
		t.thread.start();
	}

}
